home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / knuminput.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-10  |  29.0 KB  |  958 lines

  1. /*
  2.  * knuminput.h
  3.  *
  4.  *  Copyright (c) 1997 Patrick Dowler <dowler@morgul.fsh.uvic.ca>
  5.  *  Copyright (c) 2000 Dirk A. Mueller <mueller@kde.org>
  6.  *  Copyright (c) 2002 Marc Mutz <mutz@kde.org>
  7.  *
  8.  *  Requires the Qt widget libraries, available at no cost at
  9.  *  http://www.troll.no/
  10.  *
  11.  *  This library is free software; you can redistribute it and/or
  12.  *  modify it under the terms of the GNU Library General Public
  13.  *  License as published by the Free Software Foundation; either
  14.  *  version 2 of the License, or (at your option) any later version.
  15.  *
  16.  *  This library is distributed in the hope that it will be useful,
  17.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19.  *  Library General Public License for more details.
  20.  *
  21.  *  You should have received a copy of the GNU Library General Public License
  22.  *  along with this library; see the file COPYING.LIB.  If not, write to
  23.  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  24.  *  Boston, MA 02110-1301, USA.
  25.  */
  26.  
  27. #ifndef K_NUMINPUT_H
  28. #define K_NUMINPUT_H
  29.  
  30. #include <qwidget.h>
  31. #include <qspinbox.h>
  32. #include <kdelibs_export.h>
  33.  
  34. class QLabel;
  35. class QSlider;
  36. class QLineEdit;
  37. class QLayout;
  38. class QValidator;
  39.  
  40. class KIntSpinBox;
  41.  
  42. /* ------------------------------------------------------------------------ */
  43.  
  44. /**
  45.  * You need to inherit from this class if you want to implement K*NumInput
  46.  * for a different variable type
  47.  *
  48.  */
  49. class KDEUI_EXPORT KNumInput : public QWidget
  50. {
  51.     Q_OBJECT
  52.     Q_PROPERTY( QString label READ label WRITE setLabel )
  53. public:
  54.     /**
  55.      * Default constructor
  56.      * @param parent If parent is 0, the new widget becomes a top-level window. If parent is another widget, this widget becomes a child window inside parent. The new widget is deleted when its parent is deleted.
  57.      * @param name The name is sent to the QObject constructor.
  58.      */
  59.     KNumInput(QWidget* parent=0, const char* name=0);
  60.  
  61.     /**
  62.      * @param below A pointer to another KNumInput.
  63.      * @param parent parent widget
  64.      * @param name name of the widget
  65.      */
  66.     KNumInput(KNumInput* below, QWidget* parent=0, const char* name=0);
  67.     ~KNumInput();
  68.  
  69.     /**
  70.      * Sets the text and alignment of the main description label.
  71.      *
  72.      * @param label The text of the label.
  73.      *              Use QString::null to remove an existing one.
  74.      *
  75.      * @param a one of @p AlignLeft, @p AlignHCenter, YAlignRight and
  76.      *          @p AlignTop, @p AlignVCenter, @p AlignBottom.
  77.      *          default is @p AlignLeft | @p AlignTop.
  78.      *
  79.      * The vertical alignment flags have special meaning with this
  80.      * widget:
  81.      *
  82.      *     @li @p AlignTop     The label is placed above the edit/slider
  83.      *     @li @p AlignVCenter The label is placed left beside the edit
  84.      *     @li @p AlignBottom  The label is placed below the edit/slider
  85.      *
  86.      */
  87.     virtual void setLabel(const QString & label, int a = AlignLeft | AlignTop);
  88.  
  89.     /**
  90.      * @return the text of the label.
  91.      */
  92.     QString label() const;
  93.  
  94.     /**
  95.      * @return if the num input has a slider.
  96.      * @since 3.1
  97.      */
  98.     bool showSlider() const { return m_slider; }
  99.  
  100.     /**
  101.      * Sets the spacing of tickmarks for the slider.
  102.      *
  103.      * @param minor Minor tickmark separation.
  104.      * @param major Major tickmark separation.
  105.      */
  106.     void setSteps(int minor, int major);
  107.  
  108.     /**
  109.      * Specifies that this widget may stretch horizontally, but is
  110.      * fixed vertically (like QSpinBox itself).
  111.      */
  112.     QSizePolicy sizePolicy() const;
  113.  
  114.     /**
  115.      * Returns a size which fits the contents of the control.
  116.      *
  117.      * @return the preferred size necessary to show the control
  118.      */
  119.     virtual QSize sizeHint() const;
  120.  
  121. protected:
  122.     /**
  123.      * Call this function whenever you change something in the geometry
  124.      * of your KNumInput child.
  125.      *
  126.      */
  127.     void layout(bool deep);
  128.  
  129.     /**
  130.      * You need to overwrite this method and implement your layout
  131.      * calculations there.
  132.      *
  133.      * See KIntNumInput::doLayout and KDoubleNumInput::doLayout implementation
  134.      * for details.
  135.      *
  136.      */
  137.     virtual void doLayout() = 0;
  138.  
  139.     KNumInput* m_prev, *m_next;
  140.     int m_colw1, m_colw2;
  141.  
  142.     QLabel*  m_label;
  143.     QSlider* m_slider;
  144.     QSize    m_sizeSlider, m_sizeLabel;
  145.  
  146.     int      m_alignment;
  147.  
  148. private:
  149.     void init();
  150.  
  151. protected:
  152.     virtual void virtual_hook( int id, void* data );
  153. private:
  154.     class KNumInputPrivate;
  155.     KNumInputPrivate *d;
  156. };
  157.  
  158. /* ------------------------------------------------------------------------ */
  159.  
  160. /**
  161.  * @short An input widget for integer numbers, consisting of a spinbox and a slider.
  162.  *
  163.  * KIntNumInput combines a QSpinBox and optionally a QSlider
  164.  * with a label to make an easy to use control for setting some integer
  165.  * parameter. This is especially nice for configuration dialogs,
  166.  * which can have many such combinated controls.
  167.  *
  168.  * The slider is created only when the user specifies a range
  169.  * for the control using the setRange function with the slider
  170.  * parameter set to "true".
  171.  *
  172.  * A special feature of KIntNumInput, designed specifically for
  173.  * the situation when there are several KIntNumInputs in a column,
  174.  * is that you can specify what portion of the control is taken by the
  175.  * QSpinBox (the remaining portion is used by the slider). This makes
  176.  * it very simple to have all the sliders in a column be the same size.
  177.  *
  178.  * It uses KIntValidator validator class. KIntNumInput enforces the
  179.  * value to be in the given range, and can display it in any base
  180.  * between 2 and 36.
  181.  *
  182.  * \image html kintnuminput.png "KDE Int Number Input Spinbox"
  183.  *
  184.  * @version $Id$
  185.  */
  186.  
  187. class KDEUI_EXPORT KIntNumInput : public KNumInput
  188. {
  189.     Q_OBJECT
  190.     Q_PROPERTY( int value READ value WRITE setValue )
  191.     Q_PROPERTY( int minValue READ minValue WRITE setMinValue )
  192.     Q_PROPERTY( int maxValue READ maxValue WRITE setMaxValue )
  193.     Q_PROPERTY( int referencePoint READ referencePoint WRITE setReferencePoint )
  194.     Q_PROPERTY( double relativeValue READ relativeValue WRITE setRelativeValue )
  195.     Q_PROPERTY( QString suffix READ suffix WRITE setSuffix )
  196.     Q_PROPERTY( QString prefix READ prefix WRITE setPrefix )
  197.     Q_PROPERTY( QString specialValueText READ specialValueText WRITE setSpecialValueText )
  198.  
  199. public:
  200.     /**
  201.      * Constructs an input control for integer values
  202.      * with base 10 and initial value 0.
  203.      */
  204.     KIntNumInput(QWidget *parent=0, const char *name=0);
  205.     /**
  206.      * Constructor
  207.      * It constructs a QSpinBox that allows the input of integer numbers
  208.      * in the range of -INT_MAX to +INT_MAX. To set a descriptive label,
  209.      * use setLabel(). To enforce the value being in a range and optionally to
  210.      * attach a slider to it, use setRange().
  211.      *
  212.      * @param value  initial value for the control
  213.      * @param base   numeric base used for display
  214.      * @param parent parent QWidget
  215.      * @param name   internal name for this widget
  216.      */
  217.     KIntNumInput(int value, QWidget* parent=0, int base = 10, const char *name=0);
  218.  
  219.     /**
  220.      * Constructor
  221.      *
  222.      * the difference to the one above is the "below" parameter. It tells
  223.      * this instance that it is visually put below some other KNumInput widget.
  224.      * Note that these two KNumInput's need not to have the same parent widget
  225.      * or be in the same layout group.
  226.      * The effect is that it'll adjust it's layout in correspondence
  227.      * with the layout of the other KNumInput's (you can build an arbitrary long
  228.      * chain).
  229.      *
  230.      * @param below  append KIntNumInput to the KNumInput chain
  231.      * @param value  initial value for the control
  232.      * @param base   numeric base used for display
  233.      * @param parent parent QWidget
  234.      * @param name   internal name for this widget
  235.      */
  236.     KIntNumInput(KNumInput* below, int value, QWidget* parent=0, int base = 10, const char *name=0);
  237.  
  238.     /**
  239.      * Destructor
  240.      *
  241.      *
  242.      */
  243.     virtual ~KIntNumInput();
  244.  
  245.     /**
  246.      * @return the current value.
  247.      */
  248.     int value() const;
  249.  
  250.     /**
  251.      * @return the curent value in units of the referencePoint.
  252.      * @since 3.1
  253.      */
  254.     double relativeValue() const;
  255.  
  256.     /**
  257.      * @return the current reference point
  258.      * @since 3.1
  259.      */
  260.     int referencePoint() const;
  261.  
  262.     /**
  263.      * @return the suffix displayed behind the value.
  264.      * @see setSuffix()
  265.      */
  266.     QString suffix() const;
  267.     /**
  268.      * @return the prefix displayed in front of the value.
  269.      * @see setPrefix()
  270.      */
  271.     QString prefix() const;
  272.     /**
  273.      * @return the string displayed for a special value.
  274.      * @see setSpecialValueText()
  275.      */
  276.     QString specialValueText() const;
  277.  
  278.     /**
  279.      * @param min  minimum value
  280.      * @param max  maximum value
  281.      * @param step step size for the QSlider
  282.      * @param slider whether the slider is created or not
  283.      */
  284.     void setRange(int min, int max, int step=1, bool slider=true);
  285.     /**
  286.      * Sets the minimum value.
  287.      */
  288.     void setMinValue(int min);
  289.     /**
  290.      * @return the minimum value.
  291.      */
  292.     int minValue() const;
  293.     /**
  294.      * Sets the maximum value.
  295.      */
  296.     void setMaxValue(int max);
  297.     /**
  298.      * @return the maximum value.
  299.      */
  300.     int maxValue() const;
  301.  
  302.     /**
  303.      * Sets the special value text. If set, the SpinBox will display
  304.      * this text instead of the numeric value whenever the current
  305.      * value is equal to minVal(). Typically this is used for indicating
  306.      * that the choice has a special (default) meaning.
  307.      */
  308.     void setSpecialValueText(const QString& text);
  309.  
  310.     virtual void setLabel(const QString & label, int a = AlignLeft | AlignTop);
  311.  
  312.     /**
  313.      * This method returns the minimum size necessary to display the
  314.      * control. The minimum size is enough to show all the labels
  315.      * in the current font (font change may invalidate the return value).
  316.      *
  317.      * @return the minimum size necessary to show the control
  318.      */
  319.     virtual QSize minimumSizeHint() const;
  320.  
  321. public slots:
  322.     /**
  323.      * Sets the value of the control.
  324.      */
  325.     void setValue(int);
  326.  
  327.     /**
  328.      * Sets the value in units of the referencePoint
  329.      * @since 3.1
  330.      */
  331.     void setRelativeValue(double);
  332.  
  333.     /**
  334.      * Sets the reference point for relativeValue.
  335.      * @since 3.1
  336.      */
  337.     void setReferencePoint(int);
  338.  
  339.     /**
  340.      * Sets the suffix to @p suffix.
  341.      * Use QString::null to disable this feature.
  342.      * Formatting has to be provided (e.g. a space separator between the
  343.      * prepended @p value and the suffix's text has to be provided
  344.      * as the first character in the suffix).
  345.      *
  346.      * @see QSpinBox::setSuffix(), #setPrefix()
  347.      */
  348.     void setSuffix(const QString &suffix);
  349.  
  350.     /**
  351.      * Sets the prefix to @p prefix.
  352.      * Use QString::null to disable this feature.
  353.      * Formatting has to be provided (see above).
  354.      *
  355.      * @see QSpinBox::setPrefix(), #setSuffix()
  356.      */
  357.     void setPrefix(const QString &prefix);
  358.  
  359.     /**
  360.      * sets focus to the edit widget and marks all text in if mark == true
  361.      *
  362.      */
  363.     void setEditFocus( bool mark = true );
  364.  
  365. signals:
  366.     /**
  367.      * Emitted every time the value changes (by calling setValue() or
  368.      * by user interaction).
  369.      */
  370.     void valueChanged(int);
  371.  
  372.     /**
  373.      * Emitted whenever valueChanged is. Contains the change
  374.      * relative to the referencePoint.
  375.      * @since 3.1
  376.      */
  377.     void relativeValueChanged(double);
  378.  
  379. private slots:
  380.     void spinValueChanged(int);
  381.     void slotEmitRelativeValueChanged(int);
  382.  
  383. protected:
  384.     virtual void doLayout();
  385.     void resizeEvent ( QResizeEvent * );
  386.  
  387.     KIntSpinBox* m_spin;
  388.     QSize        m_sizeSpin;
  389.  
  390. private:
  391.     void init(int value, int _base);
  392.  
  393. protected:
  394.     virtual void virtual_hook( int id, void* data );
  395. private:
  396.     class KIntNumInputPrivate;
  397.     KIntNumInputPrivate *d;
  398. };
  399.  
  400.  
  401. /* ------------------------------------------------------------------------ */
  402.  
  403. class KDoubleLine;
  404.  
  405. /**
  406.  * @short An input control for real numbers, consisting of a spinbox and a slider.
  407.  *
  408.  * KDoubleNumInput combines a QSpinBox and optionally a QSlider
  409.  * with a label to make an easy to use control for setting some float
  410.  * parameter. This is especially nice for configuration dialogs,
  411.  * which can have many such combinated controls.
  412.  *
  413.  * The slider is created only when the user specifies a range
  414.  * for the control using the setRange function with the slider
  415.  * parameter set to "true".
  416.  *
  417.  * A special feature of KDoubleNumInput, designed specifically for
  418.  * the situation when there are several instances in a column,
  419.  * is that you can specify what portion of the control is taken by the
  420.  * QSpinBox (the remaining portion is used by the slider). This makes
  421.  * it very simple to have all the sliders in a column be the same size.
  422.  *
  423.  * It uses the KDoubleValidator validator class. KDoubleNumInput
  424.  * enforces the value to be in the given range, but see the class
  425.  * documentation of KDoubleSpinBox for the tricky
  426.  * interrelationship of precision and values. All of what is said
  427.  * there applies here, too.
  428.  *
  429.  * @see KIntNumInput, KDoubleSpinBox
  430.  */
  431.  
  432. class KDEUI_EXPORT KDoubleNumInput : public KNumInput
  433. {
  434.     Q_OBJECT
  435.     Q_PROPERTY( double value READ value WRITE setValue )
  436.     Q_PROPERTY( double minValue READ minValue WRITE setMinValue )
  437.     Q_PROPERTY( double maxValue READ maxValue WRITE setMaxValue )
  438.     Q_PROPERTY( QString suffix READ suffix WRITE setSuffix )
  439.     Q_PROPERTY( QString prefix READ prefix WRITE setPrefix )
  440.     Q_PROPERTY( QString specialValueText READ specialValueText WRITE setSpecialValueText )
  441.     Q_PROPERTY( int precision READ precision WRITE setPrecision )
  442.     Q_PROPERTY( double referencePoint READ referencePoint WRITE setReferencePoint )
  443.     Q_PROPERTY( double relativeValue READ relativeValue  WRITE setRelativeValue )
  444.  
  445. public:
  446.     /**
  447.      * Constructs an input control for double values
  448.      * with initial value 0.00.
  449.      */
  450.     KDoubleNumInput(QWidget *parent=0, const char *name=0);
  451.  
  452.     /**
  453.      * @deprecated (value is rounded to a multiple of 1/100)
  454.      * Constructor
  455.      *
  456.      * @param value  initial value for the control
  457.      * @param parent parent QWidget
  458.      * @param name   internal name for this widget
  459.      */
  460.     KDoubleNumInput(double value, QWidget *parent=0, const char *name=0) KDE_DEPRECATED;
  461.  
  462.     /**
  463.      * Constructor
  464.      *
  465.      * @param lower lower boundary value
  466.      * @param upper upper boundary value
  467.      * @param value  initial value for the control
  468.      * @param step   step size to use for up/down arrow clicks
  469.      * @param precision number of digits after the decimal point
  470.      * @param parent parent QWidget
  471.      * @param name   internal name for this widget
  472.      * @since 3.1
  473.      */
  474.     KDoubleNumInput(double lower, double upper, double value, double step=0.01,
  475.             int precision=2, QWidget *parent=0, const char *name=0);
  476.  
  477.     /**
  478.      * destructor
  479.      */
  480.     virtual ~KDoubleNumInput();
  481.  
  482.     /**
  483.      * @deprecated (rounds @p value to a multiple of 1/100)
  484.      * Constructor
  485.      *
  486.      * puts it visually below other KNumInput
  487.      *
  488.      * @param  below
  489.      * @param  value  initial value for the control
  490.      * @param  parent parent QWidget
  491.      * @param  name   internal name for this widget
  492.      **/
  493.     KDoubleNumInput(KNumInput* below, double value, QWidget* parent=0, const char* name=0) KDE_DEPRECATED;
  494.  
  495.     /**
  496.      * Constructor
  497.      *
  498.      * the difference here is the "below" parameter. It tells this
  499.      * instance that it is visually put below some other KNumInput
  500.      * widget.  Note that these two KNumInput's need not to have the
  501.      * same parent widget or be in the same layout group.  The effect
  502.      * is that it'll adjust it's layout in correspondence with the
  503.      * layout of the other KNumInput's (you can build an arbitrary long
  504.      * chain).
  505.      *
  506.      * @param below  append KDoubleNumInput to the KDoubleNumInput chain
  507.      * @param lower lower boundary value
  508.      * @param upper upper boundary value
  509.      * @param value  initial value for the control
  510.      * @param step   step size to use for up/down arrow clicks
  511.      * @param precision number of digits after the decimal point
  512.      * @param parent parent QWidget
  513.      * @param name   internal name for this widget
  514.      * @since 3.1
  515.      */
  516.     KDoubleNumInput(KNumInput* below,
  517.             double lower, double upper, double value, double step=0.02,
  518.             int precision=2, QWidget *parent=0, const char *name=0);
  519.  
  520.     /**
  521.      * @return the current value.
  522.      */
  523.     double value() const;
  524.  
  525.     /**
  526.      * @return the suffix.
  527.      * @see setSuffix()
  528.      */
  529.     QString suffix() const;
  530.  
  531.     /**
  532.      * @return the prefix.
  533.      * @see setPrefix()
  534.      */
  535.     QString prefix() const;
  536.  
  537.     /**
  538.      * @return the precision.
  539.      * @see setPrecision()
  540.      */
  541.     int precision() const;
  542.  
  543.     /**
  544.      * @return the string displayed for a special value.
  545.      * @see setSpecialValueText()
  546.      */
  547.     QString specialValueText() const { return m_specialvalue; }
  548.  
  549.      /**
  550.      * @param min  minimum value
  551.      * @param max  maximum value
  552.      * @param step step size for the QSlider
  553.      * @param slider whether the slider is created or not
  554.      */
  555.     void setRange(double min, double max, double step=1, bool slider=true);
  556.     /**
  557.      * Sets the minimum value.
  558.      */
  559.     void setMinValue(double min);
  560.     /**
  561.      * @return the minimum value.
  562.      */
  563.     double minValue() const;
  564.     /**
  565.      * Sets the maximum value.
  566.      */
  567.     void setMaxValue(double max);
  568.     /**
  569.      * @return the maximum value.
  570.      */
  571.     double maxValue() const;
  572.  
  573.     /**
  574.      * Specifies the number of digits to use.
  575.      */
  576.     void setPrecision(int precision);
  577.  
  578.     /**
  579.      * @return the reference point for relativeValue calculation
  580.      * @since 3.1
  581.      */
  582.     double referencePoint() const;
  583.  
  584.     /**
  585.      * @return the current value in units of referencePoint.
  586.      * @since 3.1
  587.      */
  588.     double relativeValue() const;
  589.  
  590.     /**
  591.      * Sets the special value text. If set, the spin box will display
  592.      * this text instead of the numeric value whenever the current
  593.      * value is equal to minVal(). Typically this is used for indicating
  594.      * that the choice has a special (default) meaning.
  595.      */
  596.     void setSpecialValueText(const QString& text);
  597.  
  598.     virtual void setLabel(const QString & label, int a = AlignLeft | AlignTop);
  599.     virtual QSize minimumSizeHint() const;
  600.     virtual bool eventFilter(QObject*, QEvent*);
  601.  
  602. public slots:
  603.     /**
  604.      * Sets the value of the control.
  605.      */
  606.     void setValue(double);
  607.  
  608.     /**
  609.      * Sets the value in units of referencePoint.
  610.      * @since 3.1
  611.      */
  612.     void setRelativeValue(double);
  613.  
  614.     /**
  615.      * Sets the reference Point to @p ref. It @p ref == 0, emitting of
  616.      * relativeValueChanged is blocked and relativeValue
  617.      * just returns 0.
  618.      * @since 3.1
  619.      */
  620.     void setReferencePoint(double ref);
  621.  
  622.     /**
  623.      * Sets the suffix to be displayed to @p suffix. Use QString::null to disable
  624.      * this feature. Note that the suffix is attached to the value without any
  625.      * spacing. So if you prefer to display a space separator, set suffix
  626.      * to something like " cm".
  627.      * @see setSuffix()
  628.      */
  629.     void setSuffix(const QString &suffix);
  630.  
  631.     /**
  632.      * Sets the prefix to be displayed to @p prefix. Use QString::null to disable
  633.      * this feature. Note that the prefix is attached to the value without any
  634.      * spacing.
  635.      * @see setPrefix()
  636.      */
  637.     void setPrefix(const QString &prefix);
  638.  
  639. signals:
  640.     /**
  641.      * Emitted every time the value changes (by calling setValue() or
  642.      * by user interaction).
  643.      */
  644.     void valueChanged(double);
  645.     /**
  646.      * This is an overloaded member function, provided for
  647.      * convenience. It essentially behaves like the above function.
  648.      *
  649.      * Contains the value in units of referencePoint.
  650.      * @since 3.1
  651.      */
  652.     void relativeValueChanged(double);
  653.  
  654. private slots:
  655.     void sliderMoved(int);
  656.     void slotEmitRelativeValueChanged(double);
  657.  
  658. protected:
  659.     virtual void doLayout();
  660.     void resizeEvent ( QResizeEvent * );
  661.  
  662.     virtual void resetEditBox();
  663.  
  664.     // ### no longer used, remove when BIC allowed
  665.     KDoubleLine*   edit;
  666.  
  667.     bool     m_range;
  668.     double   m_lower, m_upper, m_step;
  669.     // ### end no longer used
  670.  
  671.     QSize    m_sizeEdit;
  672.  
  673.     friend class KDoubleLine;
  674. private:
  675.     void init(double value, double lower, double upper,
  676.           double step, int precision);
  677.     double mapSliderToSpin(int) const;
  678.     void updateLegacyMembers();
  679.     // ### no longer used, remove when BIC allowed:
  680.     QString  m_specialvalue, m_prefix, m_suffix;
  681.     double   m_value;
  682.     short    m_precision;
  683.     // ### end remove when BIC allowed
  684.  
  685. protected:
  686.     virtual void virtual_hook( int id, void* data );
  687. private:
  688.     class KDoubleNumInputPrivate;
  689.     KDoubleNumInputPrivate *d;
  690. };
  691.  
  692.  
  693. /* ------------------------------------------------------------------------ */
  694.  
  695. /**
  696.  *  @short A QSpinBox with support for arbitrary base numbers.
  697.  *
  698.  *  A QSpinBox with support for arbitrary base numbers
  699.  *  (e.g. hexadecimal).
  700.  *
  701.  *  The class provides an easy interface to use other
  702.  *  numeric systems than the decimal.
  703.  */
  704. class KDEUI_EXPORT KIntSpinBox : public QSpinBox
  705. {
  706.     Q_OBJECT
  707.     Q_PROPERTY( int base READ base WRITE setBase )
  708.  
  709. public:
  710.  
  711.     /**
  712.      *  Constructor.
  713.      *
  714.      *  Constructs a widget with an integer inputline with a little scrollbar
  715.      *  and a slider, with minimal value 0, maximal value 99, step 1, base 10
  716.      *  and initial value 0.
  717.      */
  718.     KIntSpinBox( QWidget *parent=0, const char *name=0);
  719.  
  720.     /**
  721.      *  Constructor.
  722.      *
  723.      *  Constructs a widget with an integer inputline with a little scrollbar
  724.      *  and a slider.
  725.      *
  726.      *  @param lower  The lowest valid value.
  727.      *  @param upper  The greatest valid value.
  728.      *  @param step   The step size of the scrollbar.
  729.      *  @param value  The actual value.
  730.      *  @param base   The base of the used number system.
  731.      *  @param parent The parent of the widget.
  732.      *  @param name   The Name of the widget.
  733.      */
  734.     KIntSpinBox(int lower, int upper, int step, int value, int base = 10,
  735.                 QWidget* parent = 0, const char* name = 0);
  736.  
  737.     /**
  738.      *  Destructor.
  739.      */
  740.     virtual ~KIntSpinBox();
  741.  
  742.     /**
  743.      * Sets the base in which the numbers in the spin box are represented.
  744.      */
  745.     void setBase(int base);
  746.     /**
  747.      * @return the base in which numbers in the spin box are represented.
  748.      */
  749.     int base() const;
  750.     /**
  751.      * sets focus and optionally marks all text
  752.      *
  753.      */
  754.     void setEditFocus(bool mark);
  755.  
  756. protected:
  757.  
  758.     /**
  759.      *  Overloaded the method in QSpinBox
  760.      *  to make use of the base given in the constructor.
  761.      */
  762.     virtual QString mapValueToText(int);
  763.  
  764.     /**
  765.      *  Overloaded the method in QSpinBox
  766.      *  to make use of the base given in the constructor.
  767.      */
  768.     virtual int mapTextToValue(bool*);
  769.  
  770. private:
  771.     int val_base;
  772. protected:
  773.     virtual void virtual_hook( int id, void* data );
  774. private:
  775.     class KIntSpinBoxPrivate;
  776.     KIntSpinBoxPrivate *d;
  777. };
  778.  
  779.  
  780. /* --------------------------------------------------------------------------- */
  781.  
  782. /**
  783.    @short A spin box for fractional numbers.
  784.  
  785.    This class provides a spin box for fractional numbers.
  786.  
  787.    \image html kdoublespinbox.png "KDE Fractional Number Spinbox"
  788.  
  789.    See below for code examples on how to use this class.
  790.  
  791.    \b Parameters \n
  792.  
  793.    To make successful use of KDoubleSpinBox, you need to understand the
  794.    relationship between precision and available range.
  795.  
  796.    @li precision: The number of digits after the decimal point.
  797.    @li maxValue/minValue: upper and lower bounds of the valid range
  798.    @li lineStep: the size of the step that is made when the user hits
  799.                  the up or down buttons
  800.  
  801.    Since we work with fixed-point numbers internally, the maximum
  802.    precision is a function of the valid range and vice versa. More
  803.    precisely, the following relationships hold:
  804.    \code
  805.    max( abs(minValue()), abs(maxValue() ) <= INT_MAX/10^precision
  806.    maxPrecision = floor( log10( INT_MAX/max(abs(minValue()),abs(maxValue())) ) )
  807.    \endcode
  808.  
  809.    Since the value, bounds and lineStep are rounded to the current
  810.    precision, you may find that the order of setting these
  811.    parameters matters. As an example, the following are @em not equivalent (try
  812.    it!):
  813.  
  814.    \code
  815.    // sets precision,
  816.    // then min/max value (rounded to precision and clipped to obtainable range if needed)
  817.    // then value and lineStep
  818.    KDoubleSpinBox * spin = new KDoubleSpinBox( 0, 9.999, 0.001, 4.321, 3, this );
  819.  
  820.    // sets minValue to 0; maxValue to 10.00(!); value to 4.32(!) and only then
  821.    // increases the precision - too late, since e.g. value has already been rounded...
  822.    KDoubleSpinBox * spin = new KDoubleSpinBox( this );
  823.    spin->setMinValue( 0 );
  824.    spin->setMaxValue( 9.999 );
  825.    spin->setValue( 4.321 );
  826.    spin->setPrecision( 3 );
  827.    \endcode
  828.  
  829.    @author Marc Mutz <mutz@kde.org>
  830.    @version $Id$
  831.    @since 3.1
  832. **/
  833.  
  834. class KDEUI_EXPORT KDoubleSpinBox : public QSpinBox {
  835.   Q_OBJECT
  836.   Q_PROPERTY( bool acceptLocalizedNumbers READ acceptLocalizedNumbers WRITE setAcceptLocalizedNumbers )
  837.   Q_OVERRIDE( double maxValue READ maxValue WRITE setMaxValue )
  838.   Q_OVERRIDE( double minValue READ minValue WRITE setMinValue )
  839.   Q_OVERRIDE( double lineStep READ lineStep WRITE setLineStep )
  840.   Q_OVERRIDE( double value READ value WRITE setValue )
  841.   Q_PROPERTY( int precision READ precision WRITE setPrecision )
  842.  
  843. public:
  844.   /** Constructs a KDoubleSpinBox with parent @p parent and
  845.       default values for range and value (whatever QRangeControl
  846.       uses) and precision (2). */
  847.   KDoubleSpinBox( QWidget * parent=0, const char * name=0 );
  848.  
  849.   /** Constructs a KDoubleSpinBox with parent @p parent, range
  850.       [ @p lower, @p upper ], lineStep @p step, precision @p
  851.       precision and initial value @p value. */
  852.   KDoubleSpinBox( double lower, double upper, double step, double value,
  853.           int precision=2, QWidget * parent=0, const char * name=0 );
  854.  
  855.   virtual ~KDoubleSpinBox();
  856.  
  857.   /** @return whether the spinbox uses localized numbers */
  858.   bool acceptLocalizedNumbers() const;
  859.  
  860.   /** Sets whether to use and accept localized numbers as returned by
  861.       KLocale::formatNumber() */
  862.   virtual void setAcceptLocalizedNumbers( bool accept );
  863.  
  864.   /** Sets a new range for the spin box values. Note that @p lower, @p
  865.       upper and @p step are rounded to @p precision decimal points
  866.       first. */
  867.   void setRange( double lower, double upper, double step=0.01, int precision=2 );
  868.  
  869.   /** @return the current number of digits displayed to the right of the
  870.       decimal point. */
  871.   int precision() const;
  872.  
  873.   /** Equivalent to setPrecision( @p precision, @p false ); Needed
  874.       since Qt's moc doesn't ignore trailing parameters with default
  875.       args when searching for a property setter method. */
  876.   void setPrecision( int precision );
  877.  
  878.   /** Sets the precision (number of digits to the right of the decimal point). Note
  879.       that there is a tradeoff between the precision used and the available range of
  880.       values. See the class documentation above for more information on this.
  881.  
  882.       @param precision the new precision to use
  883.  
  884.       @param force if true, disables checking of bounds violations that can
  885.              arise if you increase the precision so much that the
  886.              minimum and maximum values can't be represented
  887.              anymore. Disabling is useful if you were going to disable range
  888.              control in any case.
  889.   **/
  890.   virtual void setPrecision( int precision, bool force );
  891.  
  892.   /** @return the current value */
  893.   double value() const;
  894.  
  895.   /** @return the current lower bound */
  896.   double minValue() const;
  897.  
  898.   /** Sets the lower bound of the range to @p value, subject to the
  899.       contraints that @p value is first rounded to the current
  900.       precision and then clipped to the maximum range interval that can
  901.       be handled at that precision.
  902.       @see maxValue, minValue, setMaxValue, setRange
  903.   */
  904.   void setMinValue( double value );
  905.  
  906.   /** @return the current upper bound */
  907.   double maxValue() const;
  908.  
  909.   /** Sets the upper bound of the range to @p value, subject to the
  910.       contraints that @p value is first rounded to the current
  911.       precision and then clipped to the maximum range interval
  912.       that can be handled at that precision.
  913.       @see minValue, maxValue, setMinValue, setRange
  914.   */
  915.   void setMaxValue( double value );
  916.  
  917.   /** @return the current step size */
  918.   double lineStep() const;
  919.  
  920.   /** Sets the step size for clicking the up/down buttons to @p step,
  921.       subject to the constraints that @p step is first rounded to the
  922.       current precision and then clipped to the meaningful interval
  923.       [ 1, @p maxValue() - @p minValue() ]. */
  924.   void setLineStep( double step );
  925.  
  926.   /** Overridden to ignore any setValidator() calls. */
  927.   void setValidator( const QValidator * );
  928.  
  929. signals:
  930.   /** Emitted whenever QSpinBox::valueChanged( int ) is emitted. */
  931.   void valueChanged( double value );
  932.  
  933. public slots:
  934.   /** Sets the current value to @p value, subject to the constraints
  935.       that @p value is first rounded to the current precision and then
  936.       clipped to the interval [ @p minValue() , @p maxValue() ]. */
  937.   virtual void setValue( double value );
  938.  
  939. protected:
  940.   virtual QString mapValueToText(int);
  941.   virtual int mapTextToValue(bool*);
  942.  
  943. protected slots:
  944.   void slotValueChanged( int value );
  945.  
  946. protected:
  947.  virtual void virtual_hook( int id, void* data );
  948. private:
  949.   typedef QSpinBox base;
  950.   void updateValidator();
  951.   int maxPrecision() const;
  952.  
  953.   class Private;
  954.   Private * d;
  955. };
  956.  
  957. #endif // K_NUMINPUT_H
  958.